package com.atguigu.flink0922.chapter05.transform;

import com.atguigu.flink0922.bean.WaterSensor;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/3 9:07
 */
public class Flink09_RollingSum_1 {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        // select id, 常量, 聚合函数   from .. group by id
        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 50));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 30));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));
    
        // 统计每个传感器的水位和!
        env
            .fromCollection(waterSensors)
            // .keyBy( value -> value.getId())   方法引用
            .keyBy(WaterSensor::getId)
            //.sum("vc")
            //.max("vc")
            //.min("vc")
            .maxBy("vc", false)
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
1. 聚合的时候, 处理分组字段和聚合字段, 其他字段选择的是第一次碰到的那个值

 */